package com.zzyl.controller.security;

import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/role")
@Api(tags = "角色管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping(path = "/page/{pageNum}/{pageSize}")
    @ApiOperation(value = "分页查询角色")
    public ResponseResult<PageResponse<RoleVo>> selectRolePage(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @RequestBody RoleDto roleDto) {
        return ResponseResult.success(roleService.selectRolePage(pageNum, pageSize, roleDto));
    }

    @PutMapping
    @ApiOperation(value = "新增角色")
    public ResponseResult<Void> addRole(@RequestBody RoleDto roleDto) {
        roleService.addRole(roleDto);
        return ResponseResult.success();
    }

    @GetMapping(path = "/find-checked-resources/{roleId}")
    @ApiOperation("查询角色已关联的资源列表")
    public ResponseResult<List<String>> findCheckedResources(@PathVariable Long roleId) {
        return ResponseResult.success(roleService.findCheckedResources(roleId));
    }

    @PatchMapping
    @ApiOperation("修改角色")
    public ResponseResult<Void> updateRole(@RequestBody RoleDto roleDto) {
        roleService.updateRole(roleDto);
        return ResponseResult.success();
    }

    @DeleteMapping(path = "/{roleId}")
    @ApiOperation("删除角色")
    public ResponseResult<Void> deleteByIds(@PathVariable("roleId") Long roleId) {
        roleService.deleteById(roleId);
        return ResponseResult.success();
    }

    @PostMapping(path = "/init-roles")
    @ApiOperation("查询所有角色")
    public ResponseResult<List<RoleVo>> initRoles() {
        List<RoleVo> list = roleService.initRoles();
        return ResponseResult.success(list);
    }
}
